# 测试数据库连接和数据
Write-Host "=== 测试数据库连接和数据 ===" -ForegroundColor Green

# 1. 测试后端服务状态
Write-Host "`n1. 测试后端服务状态..." -ForegroundColor Yellow
try {
    $response = Invoke-RestMethod -Uri "http://localhost:5252/api/auth/validate" -Method GET -ErrorAction Stop
    Write-Host "✓ 后端服务正常运行" -ForegroundColor Green
} catch {
    Write-Host "✗ 后端服务连接失败: $($_.Exception.Message)" -ForegroundColor Red
    exit 1
}

# 2. 测试登录功能
Write-Host "`n2. 测试登录功能..." -ForegroundColor Yellow
try {
    $loginData = @{
        username = "admin"
        password = "password"
    } | ConvertTo-Json
    
    $loginResponse = Invoke-RestMethod -Uri "http://localhost:5252/api/auth/login" -Method POST -Body $loginData -ContentType "application/json"
    
    if ($loginResponse.token) {
        Write-Host "✓ 登录成功，获取到Token" -ForegroundColor Green
        $token = $loginResponse.token
    } else {
        Write-Host "✗ 登录失败，未获取到Token" -ForegroundColor Red
        exit 1
    }
} catch {
    Write-Host "✗ 登录失败: $($_.Exception.Message)" -ForegroundColor Red
    exit 1
}

# 3. 测试角色列表API
Write-Host "`n3. 测试角色列表API..." -ForegroundColor Yellow
try {
    $headers = @{
        "Authorization" = "Bearer $token"
        "Content-Type" = "application/json"
    }
    
    $roleResponse = Invoke-RestMethod -Uri "http://localhost:5252/api/role/list" -Method GET -Headers $headers
    Write-Host "✓ 角色列表API正常，返回数据: $($roleResponse.Count) 条记录" -ForegroundColor Green
    
    if ($roleResponse.Count -gt 0) {
        Write-Host "角色数据:" -ForegroundColor Cyan
        $roleResponse | ForEach-Object { Write-Host "  - $($_.Name): $($_.Description)" -ForegroundColor White }
    } else {
        Write-Host "⚠ 角色列表为空" -ForegroundColor Yellow
    }
} catch {
    Write-Host "✗ 角色列表API失败: $($_.Exception.Message)" -ForegroundColor Red
}

# 4. 测试权限列表API
Write-Host "`n4. 测试权限列表API..." -ForegroundColor Yellow
try {
    $permissionResponse = Invoke-RestMethod -Uri "http://localhost:5252/api/permission/list" -Method GET -Headers $headers
    Write-Host "✓ 权限列表API正常，返回数据: $($permissionResponse.Count) 条记录" -ForegroundColor Green
    
    if ($permissionResponse.Count -gt 0) {
        Write-Host "权限数据:" -ForegroundColor Cyan
        $permissionResponse | ForEach-Object { Write-Host "  - $($_.Name): $($_.Description)" -ForegroundColor White }
    } else {
        Write-Host "⚠ 权限列表为空" -ForegroundColor Yellow
    }
} catch {
    Write-Host "✗ 权限列表API失败: $($_.Exception.Message)" -ForegroundColor Red
}

# 5. 测试文章列表API
Write-Host "`n5. 测试文章列表API..." -ForegroundColor Yellow
try {
    $articleResponse = Invoke-RestMethod -Uri "http://localhost:5252/api/article/list" -Method GET -Headers $headers
    Write-Host "✓ 文章列表API正常，返回数据: $($articleResponse.Count) 条记录" -ForegroundColor Green
    
    if ($articleResponse.Count -gt 0) {
        Write-Host "文章数据:" -ForegroundColor Cyan
        $articleResponse | ForEach-Object { Write-Host "  - $($_.Title): $($_.Status)" -ForegroundColor White }
    } else {
        Write-Host "⚠ 文章列表为空" -ForegroundColor Yellow
    }
} catch {
    Write-Host "✗ 文章列表API失败: $($_.Exception.Message)" -ForegroundColor Red
}

# 6. 测试用户列表API
Write-Host "`n6. 测试用户列表API..." -ForegroundColor Yellow
try {
    $userResponse = Invoke-RestMethod -Uri "http://localhost:5252/api/user" -Method GET -Headers $headers
    Write-Host "✓ 用户列表API正常，返回数据: $($userResponse.total) 条记录" -ForegroundColor Green
    
    if ($userResponse.data.Count -gt 0) {
        Write-Host "用户数据:" -ForegroundColor Cyan
        $userResponse.data | ForEach-Object { Write-Host "  - $($_.Username): $($_.Email)" -ForegroundColor White }
    } else {
        Write-Host "⚠ 用户列表为空" -ForegroundColor Yellow
    }
} catch {
    Write-Host "✗ 用户列表API失败: $($_.Exception.Message)" -ForegroundColor Red
}

Write-Host "`n=== 测试完成 ===" -ForegroundColor Green 